<?php
require_once('controller/login.php');

function show_page($page) 														// ----[PAGE START]---
{

    $template='';
    $posts_per_page = 5;

    $template = "view/templates/default_template.php";

    if (file_exists($template) && is_file($template))
    {
        require_once($template);
    }
    else
    {
        require_once("view/templates/default_template.php");
    }

    echo get_document_head();
	
    echo get_header();

    echo get_wrapper();

    echo get_leftsidebar();

    echo get_login();   

    echo get_leftsidebar_end();

 	if ($page=="startpage") {
 		if (isset($_SESSION['user_id']))
 			$user_id = $_SESSION['user_id'];
 		echo get_startpage();
 	} else if ($page=="game") {
 		echo "gamepage";
 	} else if ($page=="user") {
 		echo "userpage";
 	} else {
 		if (isset($_SESSION['user_id']))
 			$user_id = $_SESSION['user_id'];
 		echo get_startpage();
 	}

    echo get_wrapper_end();
	echo get_footer();
    echo get_document_end();
}

function show_game_page($page) 														// ----[SHOW GAME START]---
{

    $template='';
    $posts_per_page = 5;

    $template = "view/templates/default_game_template.php";

    if (file_exists($template) && is_file($template))
    {
        require_once($template);
    }
    else
    {
        require_once("view/templates/default__game_template.php");
    }
	echo get_themes();
	
    echo get_document_head();

    echo get_header();
	
    echo get_wrapper();


    if ($page=="game") {
    	$game_id = $_GET['gameid'];
    	
    	$chat_room_id = select_from_db(array('id'), array('chat_rooms'),  array("game_id = $game_id"));
    	$user_id = $_SESSION['user_id'];
    	
    	echo get_game($game_id,$chat_room_id[0]['id'],$user_id);
    }elseif ($page=="newmap"){
		echo get_newmap();
    }elseif ($page=="addunits"){
		$map_id = $_GET['mapid'];
		echo get_addunits($map_id); 
	}elseif ($page=="addneighbors"){
		$map_id = $_GET['mapid'];
		echo get_addneighbors($map_id);
    } else {
	    echo get_leftsidebar();
		    echo get_menu();
	    echo get_leftsidebar_end();

	 	if ($page=="startpage") {
			$user_id = $_SESSION['user_id'];
	 		echo get_startpage($user_id);
	 	} elseif ($page=="uploadavatar") {
			$user_id = $_SESSION['user_id'];
			
			$user_info = select_from_db(array('alias,avatar'),array('users'),array("id=$user_id"));
			$user_alias = $user_info[0]['alias'];
			$user_avatar = $user_info[0]['avatar'];

	 		echo get_uploadavatar($user_id,$user_alias,$user_avatar);
	 	} elseif ($page=="userinfo") {	
			$user_id =  isset($_GET['u'])?$_GET['u']:0;
						
			$user_info = select_from_db(array('*'),array('users'),array("id=$user_id"));
			$user_alias = $user_info[0]['alias'];
			$user_avatar = $user_info[0]['avatar'];
			
			$user_firstname = $user_info[0]['first_name'];
			$user_lastname = $user_info[0]['last_name'];
			$user_email = $user_info[0]['email'];
			$user_country = $user_info[0]['country'];
			$user_city = $user_info[0]['city'];
			$user_birthyear = date('Y')-$user_info[0]['birth_date'];
			$user_skype = $user_info[0]['skype'];
			$user_in_game = select_from_db(array('games.name, games.id'),array('games','users__got__games'),array("users__got__games.user_id=$user_id", 'users__got__games.game_id=games.id'));
	 		$games_list="";
			foreach($user_in_game as $game_name){
				$games_list .= "<li>". $game_name['name'] . "</li>" . ", ";//<a href='index.php?p=game_lobby&g=". $game_name['id'] ."'></a>
			}
			echo get_userpage($user_alias, $user_avatar, $user_firstname, $user_lastname, $user_email, $user_country, $user_city, $user_birthyear, $user_skype, $games_list);
			
	 	} else if ($page=="mygames") {
//	 		$user_id = $_SESSION['user_id'];
//
//	 		echo get_my_games_start();
//
//	 		echo get_my_games_end();
	 	} else if ($page=="user") {
	 		echo "userpage";
		} elseif ($page=="creategame"){
			echo get_creategame();
	 	} elseif ($page=="chat") {
	  		//require_once('chat.php');
	 		echo get_main_chat($_SESSION['user_id'],1,0);
	 	} elseif ($page=="games") {
	 		$user_id = $_SESSION['user_id'];

	 		echo get_games_start();
	 		
	 		$games = select_from_db(array('id,name,start_time,round_time'), array('games, users__got__games'),
	                               array(" users__got__games.user_id = $user_id AND users__got__games.game_id = games.id"),"","");
	 		foreach ($games as $game)
		    {
		    	echo get_my_games_list($game['id'], $game['name'],$game['start_time'],$game['round_time']);
		    }
	 		echo get_games_breake();
		    
	        $games = select_from_db(array('id, name, round_time, start_time'), array('games'),
	                               array("id NOT IN (
											SELECT id
											FROM games, users__got__games
											WHERE start_time >= NOW( )
											AND users__got__games.user_id = $user_id
											AND users__got__games.game_id = games.id
											)"),"","");
	 		foreach ($games as $game)
		    {
		    	echo get_coming_games_list($game['id'], $game['name'],$game['start_time'],$game['round_time']);
		    }
	 		echo get_games_end();
	 	} elseif ($page=="game_lobby") {
	 		$user_id = $_SESSION['user_id'];
	 		echo get_game_lobby_start();
	 		$game_id = $_GET['g'];
	        $game = select_from_db(array('name,start_time,round_time,owner_id'), array('games'),
	                               array("id = $game_id"));

	    	echo get_game_info($game_id,$game[0]['name'],$game[0]['start_time'],$game[0]['round_time']);

	        $players = select_from_db(array('alias,user_id'), array('users,users__got__games'), array("users__got__games.game_id = $game_id AND users__got__games.user_id = users.id"));
		    foreach ($players as $player) {
		    	echo get_users_in_game($player['user_id'],$player['alias']);
		    }

		    $chat_room_id = select_from_db(array('id'), array('chat_rooms'),  array("game_id = $game_id"));

		    echo get_game_lobby_chat($game_id, $chat_room_id[0]['id'], $user_id);

	 		echo get_game_lobby_end();
	 	} else {
			$user_id = $_SESSION['user_id'];
	 		echo get_startpage($user_id);
	 	}
    }
	if ($page=="startpage" || $page=="games"){
		echo get_rightsidebar();

		$user_info = select_from_db(array('visits,last_visit'), array('users'),  array("id = $user_id"));
		$visits = $user_info[0]['visits'];
		$last_visit = $user_info[0]['last_visit'];
		
		echo get_userinfo($_SESSION['user_name'],$_SESSION['user_id'],$visits,$last_visit);


		echo get_rightsidebar_end();
	}

    echo get_wrapper_end();
	if ($page=="startpage" || $page=="games" || $page=="creategame"){
		echo get_footer();
	}
    echo get_document_end();
}